package com.skivingcloud.report.datas.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.skivingcloud.admin.annotation.UserOperateLog;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.common.exception.BusinessException;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.R;
import com.skivingcloud.report.datas.entity.ReportDataSource;
import com.skivingcloud.report.datas.service.IReportDataSourceService;

/**
 * <p>
 * 数据源管理 前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-07-20
 */
@RestController
@RequestMapping("/datas/reportDataSource")
public class ReportDataSourceController {
	private static Logger log = LoggerFactory.getLogger(ReportDataSourceController.class);

	@Autowired
	private IReportDataSourceService reportDataSourceService;

	/**
	 * 分页
	 */
	@PostMapping("/list")
	public R list(@RequestBody Map<String, Object> params) {
		/*Map<String, Object> params = new HashMap<>();
		params.put("page", page);
		params.put("limit", limit);
		params.put("dataForm", dataForm);*/
		PageUtils pages = reportDataSourceService.queryPage(params);
		return R.ok().put("page", pages);
	}
	
	/**
	 * 根据主键查询
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) throws Exception {
		ReportDataSource reportDataSource = reportDataSourceService.selectById(id);
		return R.ok().put("data", reportDataSource);
	}

	/**
	 * 新增
	 * 
	 * @param sysBusinessType
	 * @return
	 */
	@UserOperateLog(module = "报表管理", methods = "save", description = "新增数据源数据")
	@PostMapping("/save")
	public R save(@RequestBody ReportDataSource reportDataSource) {
		try {
			reportDataSourceService.add(reportDataSource, UserUtil.getUser());
			return R.ok();
		} catch (Exception e) {
			log.error("保存数据源数据失败!", e);
			return R.error("保存数据源数据失败!");
		}
	}

	/**
	 * 修改
	 * 
	 * @param sysBusinessType
	 * @return
	 */
	@UserOperateLog(module = "报表管理", methods = "update", description = "修改数据源数据")
	@PostMapping("/update")
	public R update(@RequestBody ReportDataSource reportDataSource) {
		try {
			reportDataSourceService.update(reportDataSource, UserUtil.getUser());
			return R.ok();
		} catch (Exception e) {
			log.error("保存数据源数据失败!", e);
			return R.error("保存数据源数据失败!");
		}
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 * @return
	 */
	@UserOperateLog(module = "报表管理", methods = "delete", description = "删除数据源数据")
	@PostMapping("/delete")
	public R delete(@RequestBody String[] ids) {
		try {
			reportDataSourceService.deleteBatch(ids);
			return R.ok();
		} catch (Exception e) {
			log.error("删除数据源数据失败!", e);
			return R.error("删除数据源数据失败!");
		}
	}

	/**
     * 测试 连接
     * @param reportDataSource
     * @return
     */
    @PostMapping("/test")
    public R testConnection(@RequestBody ReportDataSource reportDataSource) {
		try {
			boolean test = reportDataSourceService.testConnection(reportDataSource);
			if(test){
				return R.ok();
			}else{
				return R.error("测试连接失败");
			}
		} catch (BusinessException e) {
			log.error(e.getMessage(), e);
			return R.error(e.getMessage());
		} catch (Exception e) {
			log.error("测试连接失败!", e);
			return R.error("测试连接失败");
		}
    }

	/**
	 * 查询全部数据源{key,value}
	 * 
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/kvAll")
	public R kvAll() throws Exception {
		List<ReportDataSource> list = reportDataSourceService.selectAll();
		List<Map<String, Object>> kvList = list.stream().map(ds -> {
			Map<String, Object> map = new HashMap<>();
			map.put("key", ds.getSourceCode());
			map.put("value", ds.getSourceName());
			return map;
		}).collect(Collectors.toList());
		return R.ok().put("list", kvList);
	}
}
